package com.gylang.commons.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.UUID;

/**
 * @author gylang,
 * date 2020/5/4,
 * @version 1.0
 */
public class UuidUtils {
    private static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyyMMddHHmmssSSS";

    public static String uuid32() {

        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String uuid32AddTime() {

        String timeStamp = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS_SSS).format(LocalDateTime.now());
        return UUID.randomUUID().toString().replace("-", "") + timeStamp;
    }

    public static String uuid6Code() {
        // 48-57 ->[0-9]     mode = 0
        // 65-90 -> [A-Z]    mode = 1
        // 97-122 ->[a-z]    mode = 2
        //选择类型
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        int size = 6;
        for (int i = 0; i < size; i++) {
            //生成模式 [0-9] / [A-Z] / [a-z]
            int mode = random.nextInt(3);
            if (0 == mode) {
                char randomChar = (char) (random.nextInt(9) + 48);
                str.append(randomChar);
            } else if (1 == mode) {
                char randomChar = (char) (random.nextInt(26) + 65);
                str.append(randomChar);
            } else {
                char randomChar = (char) (random.nextInt(26) + 97);
                str.append(randomChar);
            }
        }
        return str.toString();
    }


    public static void main(String[] args) throws InterruptedException {
        while (true) {
            Thread.sleep(300);
            System.out.println(UuidUtils.uuid6Code());
        }
    }


    public static String defaultNickname() {

        return "用户_" + System.currentTimeMillis();
    }
}
